package org.boulderfoodrescue.webapp.client.pickuptimes;

import java.util.Date;

import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.RecurringEvent;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

public class TimeSlotsTableWeekly extends FlowPanel{
	protected CellTable<RecurringEvent> table;
	protected ListDataProvider<RecurringEvent> dataProvider;
	public TimeSlotsTableWeekly(){
		dataProvider = new ListDataProvider<RecurringEvent>();
		table = new CellTable<RecurringEvent>();

		DateCell startTimeCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT));
		Column<RecurringEvent, Date> startTimeColumn = new Column<RecurringEvent, Date>(startTimeCell){
			@Override
			public Date getValue(RecurringEvent object) {
				return object.getStartTime();
			}
		};

		DateCell endTimeCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT));
		Column<RecurringEvent, Date> endTimeColumn = new Column<RecurringEvent, Date>(endTimeCell){
			@Override
			public Date getValue(RecurringEvent object) {
				return object.getEndTime();
			}
		};
		
		DateCell startDateCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT));
		Column<RecurringEvent, Date> startDateColumn = new Column<RecurringEvent, Date>(startDateCell){
			@Override
			public Date getValue(RecurringEvent object) {
				return object.getStartTime();
			}
		};
		
		TextColumn<RecurringEvent> endDateColumn = new TextColumn<RecurringEvent>(){
			@Override
			public String getValue(RecurringEvent object) {
				switch(object.getEndType())
				{
				case Never: 
					return "Never";
				case AfterNumOccurrences:
					return object.getNumberOfOccurences() + " occurences";
				case On: 
					return DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(object.getEndDate());
				}
				return "Error recurrance type not set";
			}
		};

		TextColumn<RecurringEvent> daysColumn = new TextColumn<RecurringEvent>(){
			@Override
			public String getValue(RecurringEvent object) {
				return object.getDaysOfWeek();
			}
		};
		
		TextColumn<RecurringEvent> frequencyColumn = new TextColumn<RecurringEvent>(){
			@Override
			public String getValue(RecurringEvent object) {
				if(object.getFrequency() == null)
					return "Every Week";
				return "Every "+object.getFrequency()+" Weeks";
			}
		};

		ClickableTextCell editCell = new ClickableTextCell();
		Column<RecurringEvent, String> editColumn = new Column<RecurringEvent, String>(editCell){
			@Override
			public String getValue(RecurringEvent object) {
				return "delete";
			}
		};
		editColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

		table.addColumn(startTimeColumn, "Start Time");
		table.addColumn(endTimeColumn, "End Time");
		table.addColumn(startDateColumn, "Start Date");
		table.addColumn(endDateColumn, "Ends");
		table.addColumn(daysColumn, "Days");
		table.addColumn(frequencyColumn, "Frequency");
		table.addColumn(editColumn, "Edit");

		dataProvider.addDataDisplay(table);

		ListHandler<RecurringEvent> sortHandler = new ListHandler<RecurringEvent>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		editColumn.setFieldUpdater(new FieldUpdater<RecurringEvent, String>(){
			@Override
			public void update(int index, final RecurringEvent object, String value) {
				Window.alert("To Do");
			}
		});

		this.add(table);
		table.setWidth("100%");
		table.setEmptyTableWidget(new HTML("No time slots"));

//		if(users != null)
//			dataProvider.getList().addAll(users);
	}
	public void addRecurringEvent(RecurringEvent event){
		dataProvider.getList().add(event);
	}
	public void removeRecurringEvent(RecurringEvent event){
		dataProvider.getList().remove(event);
	}
	public void refresh()
	{
		table.redraw();
	}
}

